
<!DOCTYPE HTML>
<html lang="zh-hans" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>Modules · Gitbook demo</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        <meta name="author" content="lanyue">
        
        
    
    <link rel="stylesheet" href="gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="structure.html" />
    
    
    <link rel="prev" href="actions.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="输入并搜索" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="./">
            
                <a href="./">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/tree/1.0/docs">
            
                    
                    寻找 1.0 文档？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" >
            
                <a target="_blank" href="https://github.com/vuejs/vuex/releases">
            
                    
                    更新记录
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="installation.html">
            
                <a href="installation.html">
            
                    
                    安装
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="intro.html">
            
                <a href="intro.html">
            
                    
                    Vuex 是什么？
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="getting-started.html">
            
                <a href="getting-started.html">
            
                    
                    开始
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" >
            
                <span>
            
                    
                    核心概念
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="state.html">
            
                <a href="state.html">
            
                    
                    State
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="getters.html">
            
                <a href="getters.html">
            
                    
                    Getters
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="mutations.html">
            
                <a href="mutations.html">
            
                    
                    Mutations
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="actions.html">
            
                <a href="actions.html">
            
                    
                    Actions
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.7.5" data-path="modules.html">
            
                <a href="modules.html">
            
                    
                    Modules
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="structure.html">
            
                <a href="structure.html">
            
                    
                    项目结构
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="plugins.html">
            
                <a href="plugins.html">
            
                    
                    插件
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="strict.html">
            
                <a href="strict.html">
            
                    
                    严格模式
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="forms.html">
            
                <a href="forms.html">
            
                    
                    表单处理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="testing.html">
            
                <a href="testing.html">
            
                    
                    测试
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="hot-reload.html">
            
                <a href="hot-reload.html">
            
                    
                    热重载
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="api.html">
            
                <a href="api.html">
            
                    
                    API 文档
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            本书使用 GitBook 发布
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="." >Modules</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="modules">Modules</h1>
<p>&#x4F7F;&#x7528;&#x5355;&#x4E00;&#x72B6;&#x6001;&#x6811;&#xFF0C;&#x5BFC;&#x81F4;&#x5E94;&#x7528;&#x7684;&#x6240;&#x6709;&#x72B6;&#x6001;&#x96C6;&#x4E2D;&#x5230;&#x4E00;&#x4E2A;&#x5F88;&#x5927;&#x7684;&#x5BF9;&#x8C61;&#x3002;&#x4F46;&#x662F;&#xFF0C;&#x5F53;&#x5E94;&#x7528;&#x53D8;&#x5F97;&#x5F88;&#x5927;&#x65F6;&#xFF0C;store &#x5BF9;&#x8C61;&#x4F1A;&#x53D8;&#x5F97;&#x81C3;&#x80BF;&#x4E0D;&#x582A;&#x3002;</p>
<p>&#x4E3A;&#x4E86;&#x89E3;&#x51B3;&#x4EE5;&#x4E0A;&#x95EE;&#x9898;&#xFF0C;Vuex &#x5141;&#x8BB8;&#x6211;&#x4EEC;&#x5C06; store &#x5206;&#x5272;&#x5230;<strong>&#x6A21;&#x5757;&#xFF08;module&#xFF09;</strong>&#x3002;&#x6BCF;&#x4E2A;&#x6A21;&#x5757;&#x62E5;&#x6709;&#x81EA;&#x5DF1;&#x7684; state&#x3001;mutation&#x3001;action&#x3001;getters&#x3001;&#x751A;&#x81F3;&#x662F;&#x5D4C;&#x5957;&#x5B50;&#x6A21;&#x5757;&#x2014;&#x2014;&#x4ECE;&#x4E0A;&#x81F3;&#x4E0B;&#x8FDB;&#x884C;&#x7C7B;&#x4F3C;&#x7684;&#x5206;&#x5272;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> moduleA = {
  state: { ... },
  mutations: { ... },
  actions: { ... },
  getters: { ... }
}

<span class="hljs-keyword">const</span> moduleB = {
  state: { ... },
  mutations: { ... },
  actions: { ... }
}

<span class="hljs-keyword">const</span> store = <span class="hljs-keyword">new</span> Vuex.Store({
  modules: {
    a: moduleA,
    b: moduleB
  }
})

store.state.a <span class="hljs-comment">// -&gt; moduleA &#x7684;&#x72B6;&#x6001;</span>
store.state.b <span class="hljs-comment">// -&gt; moduleB &#x7684;&#x72B6;&#x6001;</span>
</code></pre>
<h3 id="&#x6A21;&#x5757;&#x7684;&#x5C40;&#x90E8;&#x72B6;&#x6001;">&#x6A21;&#x5757;&#x7684;&#x5C40;&#x90E8;&#x72B6;&#x6001;</h3>
<p>&#x5BF9;&#x4E8E;&#x6A21;&#x5757;&#x5185;&#x90E8;&#x7684; mutation &#x548C; getter&#xFF0C;&#x63A5;&#x6536;&#x7684;&#x7B2C;&#x4E00;&#x4E2A;&#x53C2;&#x6570;&#x662F;<strong>&#x6A21;&#x5757;&#x7684;&#x5C40;&#x90E8;&#x72B6;&#x6001;</strong>&#x3002;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> moduleA = {
  state: { count: <span class="hljs-number">0</span> },
  mutations: {
    increment (state) {
      <span class="hljs-comment">// state &#x6A21;&#x5757;&#x7684;&#x5C40;&#x90E8;&#x72B6;&#x6001;</span>
      state.count++
    }
  },

  getters: {
    doubleCount (state) {
      <span class="hljs-keyword">return</span> state.count * <span class="hljs-number">2</span>
    }
  }
}
</code></pre>
<p>&#x540C;&#x6837;&#xFF0C;&#x5BF9;&#x4E8E;&#x6A21;&#x5757;&#x5185;&#x90E8;&#x7684; action&#xFF0C;<code>context.state</code> &#x662F;&#x5C40;&#x90E8;&#x72B6;&#x6001;&#xFF0C;&#x6839;&#x8282;&#x70B9;&#x7684;&#x72B6;&#x6001;&#x662F; <code>context.rootState</code>:</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> moduleA = {
  <span class="hljs-comment">// ...</span>
  actions: {
    incrementIfOddOnRootSum ({ state, commit, rootState }) {
      <span class="hljs-keyword">if</span> ((state.count + rootState.count) % <span class="hljs-number">2</span> === <span class="hljs-number">1</span>) {
        commit(<span class="hljs-string">&apos;increment&apos;</span>)
      }
    }
  }
}
</code></pre>
<p>&#x5BF9;&#x4E8E;&#x6A21;&#x5757;&#x5185;&#x90E8;&#x7684; getter&#xFF0C;&#x6839;&#x8282;&#x70B9;&#x72B6;&#x6001;&#x4F1A;&#x4F5C;&#x4E3A;&#x7B2C;&#x4E09;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> moduleA = {
  <span class="hljs-comment">// ...</span>
  getters: {
    sumWithRootCount (state, getters, rootState) {
      <span class="hljs-keyword">return</span> state.count + rootState.count
    }
  }
}
</code></pre>
<h3 id="&#x547D;&#x540D;&#x7A7A;&#x95F4;">&#x547D;&#x540D;&#x7A7A;&#x95F4;</h3>
<p>&#x6A21;&#x5757;&#x5185;&#x90E8;&#x7684; action&#x3001;mutation&#x3001;&#x548C; getter &#x73B0;&#x5728;&#x4ECD;&#x7136;&#x6CE8;&#x518C;&#x5728;<strong>&#x5168;&#x5C40;&#x547D;&#x540D;&#x7A7A;&#x95F4;</strong>&#x2014;&#x2014;&#x8FD9;&#x6837;&#x4FDD;&#x8BC1;&#x4E86;&#x591A;&#x4E2A;&#x6A21;&#x5757;&#x80FD;&#x591F;&#x54CD;&#x5E94;&#x540C;&#x4E00; mutation &#x6216; action&#x3002;&#x4F60;&#x53EF;&#x4EE5;&#x901A;&#x8FC7;&#x6DFB;&#x52A0;&#x524D;&#x7F00;&#x6216;&#x540E;&#x7F00;&#x7684;&#x65B9;&#x5F0F;&#x9694;&#x79BB;&#x5404;&#x6A21;&#x5757;&#xFF0C;&#x4EE5;&#x907F;&#x514D;&#x540D;&#x79F0;&#x51B2;&#x7A81;&#x3002;&#x4F60;&#x4E5F;&#x53EF;&#x80FD;&#x5E0C;&#x671B;&#x5199;&#x51FA;&#x4E00;&#x4E2A;&#x53EF;&#x590D;&#x7528;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x5176;&#x4F7F;&#x7528;&#x73AF;&#x5883;&#x4E0D;&#x53EF;&#x63A7;&#x3002;&#x4F8B;&#x5982;&#xFF0C;&#x6211;&#x4EEC;&#x60F3;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>todos</code> &#x6A21;&#x5757;&#xFF1A;</p>
<pre><code class="lang-js"><span class="hljs-comment">// types.js</span>

<span class="hljs-comment">// &#x5B9A;&#x4E49; getter&#x3001;action&#x3001;&#x548C; mutation &#x7684;&#x540D;&#x79F0;&#x4E3A;&#x5E38;&#x91CF;&#xFF0C;&#x4EE5;&#x6A21;&#x5757;&#x540D; `todos` &#x4E3A;&#x524D;&#x7F00;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> DONE_COUNT = <span class="hljs-string">&apos;todos/DONE_COUNT&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> FETCH_ALL = <span class="hljs-string">&apos;todos/FETCH_ALL&apos;</span>
<span class="hljs-keyword">export</span> <span class="hljs-keyword">const</span> TOGGLE_DONE = <span class="hljs-string">&apos;todos/TOGGLE_DONE&apos;</span>
</code></pre>
<pre><code class="lang-js"><span class="hljs-comment">// modules/todos.js</span>
<span class="hljs-keyword">import</span> * <span class="hljs-keyword">as</span> types <span class="hljs-keyword">from</span> <span class="hljs-string">&apos;../types&apos;</span>

<span class="hljs-comment">// &#x4F7F;&#x7528;&#x6DFB;&#x52A0;&#x4E86;&#x524D;&#x7F00;&#x7684;&#x540D;&#x79F0;&#x5B9A;&#x4E49; getter&#x3001;action &#x548C; mutation</span>
<span class="hljs-keyword">const</span> todosModule = {
  state: { todos: [] },

  getters: {
    [types.DONE_COUNT] (state) {
      <span class="hljs-comment">// ...</span>
    }
  },

  actions: {
    [types.FETCH_ALL] (context, payload) {
      <span class="hljs-comment">// ...</span>
    }
  },

  mutations: {
    [types.TOGGLE_DONE] (state, payload) {
      <span class="hljs-comment">// ...</span>
    }
  }
}
</code></pre>
<h3 id="&#x6A21;&#x5757;&#x52A8;&#x6001;&#x6CE8;&#x518C;">&#x6A21;&#x5757;&#x52A8;&#x6001;&#x6CE8;&#x518C;</h3>
<p>&#x5728; store &#x521B;&#x5EFA;<strong>&#x4E4B;&#x540E;</strong>&#xFF0C;&#x4F60;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>store.registerModule</code> &#x65B9;&#x6CD5;&#x6CE8;&#x518C;&#x6A21;&#x5757;&#xFF1A;</p>
<pre><code class="lang-js">store.registerModule(<span class="hljs-string">&apos;myModule&apos;</span>, {
  <span class="hljs-comment">// ...</span>
})
</code></pre>
<p>&#x6A21;&#x5757;&#x7684;&#x72B6;&#x6001;&#x5C06;&#x662F; <code>store.state.myModule</code>&#x3002;</p>
<p>&#x6A21;&#x5757;&#x52A8;&#x6001;&#x6CE8;&#x518C;&#x529F;&#x80FD;&#x53EF;&#x4EE5;&#x8BA9;&#x5176;&#x4ED6; Vue &#x63D2;&#x4EF6;&#x4E3A;&#x4E86;&#x5E94;&#x7528;&#x7684; store &#x9644;&#x52A0;&#x65B0;&#x6A21;&#x5757;&#xFF0C;&#x4EE5;&#x6B64;&#x6765;&#x5206;&#x5272; Vuex &#x7684;&#x72B6;&#x6001;&#x7BA1;&#x7406;&#x3002;&#x4F8B;&#x5982;&#xFF0C;<a href="https://github.com/vuejs/vuex-router-sync" target="_blank"><code>vuex-router-sync</code></a> &#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x96C6;&#x6210; vue-router &#x4E0E; vuex&#xFF0C;&#x7BA1;&#x7406;&#x52A8;&#x6001;&#x6A21;&#x5757;&#x7684;&#x8DEF;&#x7531;&#x72B6;&#x6001;&#x3002;</p>
<p>&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x4F7F;&#x7528; <code>store.unregisterModule(moduleName)</code> &#x52A8;&#x6001;&#x5730;&#x5378;&#x8F7D;&#x6A21;&#x5757;&#x3002;&#x6CE8;&#x610F;&#xFF0C;&#x4F60;&#x4E0D;&#x80FD;&#x4F7F;&#x7528;&#x6B64;&#x65B9;&#x6CD5;&#x5378;&#x8F7D;&#x9759;&#x6001;&#x6A21;&#x5757;&#xFF08;&#x5728;&#x521B;&#x5EFA; store &#x65F6;&#x58F0;&#x660E;&#x7684;&#x6A21;&#x5757;&#xFF09;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="actions.html" class="navigation navigation-prev " aria-label="Previous page: Actions">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="structure.html" class="navigation navigation-next " aria-label="Next page: 项目结构">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"Modules","level":"1.7.5","depth":2,"next":{"title":"项目结构","level":"1.8","depth":1,"path":"structure.md","ref":"structure.md","articles":[]},"previous":{"title":"Actions","level":"1.7.4","depth":2,"path":"actions.md","ref":"actions.md","articles":[]},"dir":"ltr"},"config":{"plugins":[],"root":"./","styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"pluginsConfig":{"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"theme":"default","author":"lanyue","pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"variables":{},"title":"Gitbook demo","language":"zh-hans","gitbook":">=3.0.0","description":"this is a test"},"file":{"path":"modules.md","mtime":"2017-02-21T09:41:56.410Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-03-06T15:58:45.473Z"},"basePath":".","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="gitbook/gitbook.js"></script>
    <script src="gitbook/theme.js"></script>
    
        
        <script src="gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

